Writing a dialog based application which accepts drag and drop

A programming tutorial by Lim, Chooi Guan

 

 

Preface

This tutorial is for developers who are interested in implementing drag and drop for their applications.  I implement drag and drop mainly for fully automation purposes rather than just regular opening of files, but it is up to you to decide how you wish to handle it.

 

 

Instructions

 

  1. First of all, create a dialog based application using Visual Studio.

 

  1. Now goto resource editor, right click the main dialog, and goto Properties. Make sure Accept files is enabled.

 

  1. Under the main dialog’s header file, add the following message map function declaration.

·         //{{AFX_MSG(blahDlg)

afx_msg void OnDropFiles( HDROP hDropInfo ); //for drag and drop

//}}AFX_MSG

 

  1. Now go back to the main dialog’s cpp file, and add the following message map entry.

·         BEGIN_MESSAGE_MAP(blahDlg, CDialog)

ON_WM_DROPFILES() // drag and drop message map entry

END_MESSAGE_MAP()

 

  1. Now add the function implementation in the main dialog’s cpp file.

·          afx_msg void blahDlg::OnDropFiles( HDROP hDropInfo ) {

// following message is shown when an object is dropped

AfxMessageBox(“This is an example of drag and drop”);

}

 

  1. Compile and build the executable.  Drag a file or folder onto the main dialog and you should get a messagebox popup.

 

 

You have now successfully implemented the drag and drop function. To extract information from the HDROP structure, use the following function;

 

DragQueryFile() – Queries the HDROPINFO structure

 

For example, to get the number of items dropped, you would use the following statement;

 

int iObjectCount = DragQueryFile(hDropInfo,0xFFFFFFFF,NULL,NULL);

 

Look up http://msdn.microsoft.com or the MSDN CDROM library for more information on the drag and drop query function.

 

That concludes this tutorial.  Now set forth and implement drag and drop in your applications!

 

 http://famousguy.cjb.net

Privacy Policy | Skyjuice Software
Website Content Protection